(function($) {
    'use strict';

    $(function() {
        var $fullText = $('.admin-fullText');
        $('#admin-fullscreen').on('click', function() {
            $.AMUI.fullscreen.toggle();
        });

        $(document).on($.AMUI.fullscreen.raw.fullscreenchange, function() {
            $fullText.text($.AMUI.fullscreen.isFullscreen ? '退出全屏' : '开启全屏');
        });
    });
})(jQuery);

function loading() {
    layer.load();
}

function endLoading() {
    layer.closeAll('loading');
}

function showWeb(url) {
    layer.open({
        type: 2,
        area: ['700px', '530px'],
        fix: true, //不固定
        maxmin: true,
        shadeClose: true,
        content: url
    });
    return false;
}

function msg(message) {
    layer.open({
        content: message,
        btn: '确定'
    });
}

function confirm(msg, func) {
    layer.open({
        content: msg,
        btn: ['确定', '取消'],
        yes: function(index) {
            func();
            layer.close(index);
        }
    });
}

/***
 * 乘法，获取精确乘法的结果值
 * @param arg1
 * @param arg2
 * @returns
 */
function accMul(arg1, arg2) {
    var m = 0,
        s1 = arg1.toString(),
        s2 = arg2.toString();
    try { m += s1.split(".")[1].length } catch (e) {};
    try { m += s2.split(".")[1].length } catch (e) {};
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}

/***
 * 除法，获取精确乘法的结果值
 * @param arg1
 * @param arg2
 * @returns
 */
function accDivCoupon(arg1, arg2) {
    var t1 = 0,
        t2 = 0,
        r1, r2;
    try { t1 = arg1.toString().split(".")[1].length; } catch (e) {}
    try { t2 = arg2.toString().split(".")[1].length; } catch (e) {}
    with(Math) {
        r1 = Number(arg1.toString().replace(".", ""));
        r2 = Number(arg2.toString().replace(".", ""));
        return (r1 / r2) * pow(10, t2 - t1);
    }
}

function getQueryString(name) {
    var result = window.location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
    if (result == null || result.length < 1) {
        return "";
    }
    return result[1];
}

function goPage(page) {
    goPage(page, null)
}

function goPage(page, keys) {
    var currentPage = getQueryString('page')
    var url = window.location.pathname + window.location.search
    if (window.location.search == '') {
        url = url + '?page=' + page;
    } else {
        if (url.indexOf("page=") != -1) {
            url = url.replace("page=" + currentPage, "page=" + page)
        } else {
            url = url + "&page=" + page
        }
    }
    if (keys != null) {
        for (var i = 0; i < keys.length; i++) {
            // console.log(i, array[i])
            var item = keys[i];
            var key = item.key;
            var value = getQueryString(key)
            if (url.indexOf(key + "=") == -1) {
                url = url + "&" + key + '=' + item.value
            } else {
                url = url.replace(key + "=" + value, key + "=" + item.value)
            }
        }
    }
    this.window.location = url
}

function goDownload() {
    var keys = [
        { key: 'dl', value: 1 }
    ]
    var currentPage = getQueryString('page')
    var page = currentPage
    var url = window.location.pathname + window.location.search
    if (window.location.search == '') {
        url = url + '?page=' + page;
    } else {
        if (url.indexOf("page=") != -1) {
            url = url.replace("page=" + currentPage, "page=" + page)
        } else {
            url = url + "&page=" + page
        }
    }
    if (keys != null) {
        for (var i = 0; i < keys.length; i++) {
            // console.log(i, array[i])
            var item = keys[i];
            var key = item.key;
            var value = getQueryString(key)
            if (url.indexOf(key + "=") == -1) {
                url = url + "&" + key + '=' + item.value
            } else {
                url = url.replace(key + "=" + value, key + "=" + item.value)
            }
        }
    }
    this.window.open(url)
}